<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:location:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:location:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:location:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:location:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="locationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="库位ID" align="center" prop="locationId" />
      <el-table-column label="拼音名称" align="center" prop="pyName" />
      <el-table-column label="仓库ID" align="center" prop="storageId" />
      <el-table-column label="是否锁定" align="center" prop="islock" />
      <el-table-column label="库位长度" align="center" prop="locationLong" />
      <el-table-column label="库位宽度" align="center" prop="locationWidth" />
      <el-table-column label="库位高度" align="center" prop="locationHeight" />
      <el-table-column label="体积" align="center" prop="volume" />
      <el-table-column label="已使用体积" align="center" prop="hasvolume" />
      <el-table-column label="承重" align="center" prop="bearweight" />
      <el-table-column label="ABC分类ID" align="center" prop="abcId" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kv_sts" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="版本" align="center" prop="version" />
      <el-table-column label="操作员ID" align="center" prop="operatorid" />
      <el-table-column label="操作日期" align="center" prop="operatedate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operatedate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="堆垛机编号" align="center" prop="decid" />
      <el-table-column label="入库站台" align="center" prop="instand" />
      <el-table-column label="出库站台" align="center" prop="outstand" />
      <el-table-column label="入库输送线" align="center" prop="inline" />
      <el-table-column label="出库输送线" align="center" prop="outline" />
      <el-table-column label="行" align="center" prop="row" />
      <el-table-column label="列" align="center" prop="cln" />
      <el-table-column label="层" align="center" prop="layer" />
      <el-table-column label="深度" align="center" prop="depth" />
      <el-table-column label="入库站台1" align="center" prop="instand1" />
      <el-table-column label="入库站台2" align="center" prop="instand2" />
      <el-table-column label="出库站台1" align="center" prop="outstand1" />
      <el-table-column label="入库站台3" align="center" prop="instand3" />
      <el-table-column label="出库站台2" align="center" prop="outstand2" />
      <el-table-column label="出库站台11" align="center" prop="outstand11" />
      <el-table-column label="深度" align="center" prop="deep" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:location:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:location:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库位信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="库位ID" prop="locationId">
          <el-input v-model="form.locationId" placeholder="请输入库位ID" />
        </el-form-item>
        <el-form-item label="拼音名称" prop="pyName">
          <el-input v-model="form.pyName" placeholder="请输入拼音名称" />
        </el-form-item>
        <el-form-item label="仓库ID" prop="storageId">
          <el-input v-model="form.storageId" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="是否锁定" prop="islock">
          <el-input v-model="form.islock" placeholder="请输入是否锁定" />
        </el-form-item>
        <el-form-item label="库位长度" prop="locationLong">
          <el-input v-model="form.locationLong" placeholder="请输入库位长度" />
        </el-form-item>
        <el-form-item label="库位宽度" prop="locationWidth">
          <el-input v-model="form.locationWidth" placeholder="请输入库位宽度" />
        </el-form-item>
        <el-form-item label="库位高度" prop="locationHeight">
          <el-input v-model="form.locationHeight" placeholder="请输入库位高度" />
        </el-form-item>
        <el-form-item label="体积" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入体积" />
        </el-form-item>
        <el-form-item label="已使用体积" prop="hasvolume">
          <el-input v-model="form.hasvolume" placeholder="请输入已使用体积" />
        </el-form-item>
        <el-form-item label="承重" prop="bearweight">
          <el-input v-model="form.bearweight" placeholder="请输入承重" />
        </el-form-item>
        <el-form-item label="ABC分类ID" prop="abcId">
          <el-input v-model="form.abcId" placeholder="请输入ABC分类ID" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.kv_sts"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本" />
        </el-form-item>
        <el-form-item label="操作员ID" prop="operatorid">
          <el-input v-model="form.operatorid" placeholder="请输入操作员ID" />
        </el-form-item>
        <el-form-item label="操作日期" prop="operatedate">
          <el-date-picker clearable
            v-model="form.operatedate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择操作日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="堆垛机编号" prop="decid">
          <el-input v-model="form.decid" placeholder="请输入堆垛机编号" />
        </el-form-item>
        <el-form-item label="入库站台" prop="instand">
          <el-input v-model="form.instand" placeholder="请输入入库站台" />
        </el-form-item>
        <el-form-item label="出库站台" prop="outstand">
          <el-input v-model="form.outstand" placeholder="请输入出库站台" />
        </el-form-item>
        <el-form-item label="入库输送线" prop="inline">
          <el-input v-model="form.inline" placeholder="请输入入库输送线" />
        </el-form-item>
        <el-form-item label="出库输送线" prop="outline">
          <el-input v-model="form.outline" placeholder="请输入出库输送线" />
        </el-form-item>
        <el-form-item label="行" prop="row">
          <el-input v-model="form.row" placeholder="请输入行" />
        </el-form-item>
        <el-form-item label="列" prop="cln">
          <el-input v-model="form.cln" placeholder="请输入列" />
        </el-form-item>
        <el-form-item label="层" prop="layer">
          <el-input v-model="form.layer" placeholder="请输入层" />
        </el-form-item>
        <el-form-item label="深度" prop="depth">
          <el-input v-model="form.depth" placeholder="请输入深度" />
        </el-form-item>
        <el-form-item label="入库站台1" prop="instand1">
          <el-input v-model="form.instand1" placeholder="请输入入库站台1" />
        </el-form-item>
        <el-form-item label="入库站台2" prop="instand2">
          <el-input v-model="form.instand2" placeholder="请输入入库站台2" />
        </el-form-item>
        <el-form-item label="出库站台1" prop="outstand1">
          <el-input v-model="form.outstand1" placeholder="请输入出库站台1" />
        </el-form-item>
        <el-form-item label="入库站台3" prop="instand3">
          <el-input v-model="form.instand3" placeholder="请输入入库站台3" />
        </el-form-item>
        <el-form-item label="出库站台2" prop="outstand2">
          <el-input v-model="form.outstand2" placeholder="请输入出库站台2" />
        </el-form-item>
        <el-form-item label="出库站台11" prop="outstand11">
          <el-input v-model="form.outstand11" placeholder="请输入出库站台11" />
        </el-form-item>
        <el-form-item label="深度" prop="deep">
          <el-input v-model="form.deep" placeholder="请输入深度" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLocation, getLocation, delLocation, addLocation, updateLocation } from "@/api/system/location";

export default {
  name: "Location",
  dicts: ['kv_sts'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库位信息表格数据
      locationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        locationId: [
          { required: true, message: "库位ID不能为空", trigger: "blur" }
        ],
        pyName: [
          { required: true, message: "拼音名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询库位信息列表 */
    getList() {
      this.loading = true;
      listLocation(this.queryParams).then(response => {
        this.locationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        locationId: null,
        pyName: null,
        storageId: null,
        islock: null,
        locationLong: null,
        locationWidth: null,
        locationHeight: null,
        volume: null,
        hasvolume: null,
        bearweight: null,
        abcId: null,
        sort: null,
        status: null,
        remark: null,
        version: null,
        operatorid: null,
        operatedate: null,
        decid: null,
        instand: null,
        outstand: null,
        inline: null,
        outline: null,
        row: null,
        cln: null,
        layer: null,
        depth: null,
        instand1: null,
        instand2: null,
        outstand1: null,
        instand3: null,
        outstand2: null,
        outstand11: null,
        deep: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库位信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLocation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库位信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLocation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLocation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除库位信息编号为"' + ids + '"的数据项？').then(function() {
        return delLocation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/location/export', {
        ...this.queryParams
      }, `location_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
